<?php

require_once "core/config.php";
require_once "core/db.php";
require_once "core/application/controllers/MainController.php";

DB::getInstance()->openConnectionToGS();
$type = $_GET['type'];
if (empty($type)) {
    Main_Controller::getInstance()->echoError("Значение не может быть пустым");
}

function getLevel($exp) {
    if (!$exp) {
        return false;
    }
    if ($exp > 0 && $exp <= 650) {
        $level = 1;
    } else if ($exp < 1917) {
        $level = 2;
    } else if ($exp < 4230) {
        $level = 3;
    } else if ($exp < 8693) {
        $level = 4;
    } else if ($exp < 14583) {
        $level = 5;
    } else if ($exp < 22885) {
        $level = 6;
    } else if ($exp < 34936) {
        $level = 7;
    } else if ($exp < 54235) {
        $level = 8;
    } else if ($exp < 73125) {
        $level = 9;
    } else if ($exp < 94104) {
        $level = 10;
    } else if ($exp < 130995) {
        $level = 11;
    } else if ($exp < 169608) {
        $level = 12;
    } else if ($exp < 217175) {
        $level = 13;
    } else if ($exp < 266649) {
        $level = 14;
    } else if ($exp < 326185) {
        $level = 15;
    } else if ($exp < 392297) {
        $level = 16;
    } else if ($exp < 522080) {
        $level = 17;
    } else if ($exp < 669712) {
        $level = 18;
    } else if ($exp < 817822) {
        $level = 19;
    } else if ($exp < 1000147) {
        $level = 20;
    } else if ($exp < 1295094) {
        $level = 21;
    } else if ($exp < 1609877) {
        $level = 22;
    } else if ($exp < 2001924) {
        $level = 23;
    } else if ($exp < 2348658) {
        $level = 24;
    } else if ($exp < 2686741) {
        $level = 25;
    } else if ($exp < 3117416) {
        $level = 26;
    } else if ($exp < 3602263) {
        $level = 27;
    } else if ($exp < 4012536) {
        $level = 28;
    } else if ($exp < 4676472) {
        $level = 29;
    } else if ($exp < 5828323) {
        $level = 30;
    } else if ($exp < 6808242) {
        $level = 31;
    } else if ($exp < 8091124) {
        $level = 32;
    } else if ($exp < 9689407) {
        $level = 33;
    } else if ($exp < 12669129) {
        $level = 34;
    } else if ($exp < 16124465) {
        $level = 35;
    } else if ($exp < 19742022) {
        $level = 36;
    } else if ($exp < 26021841) {
        $level = 37;
    } else if ($exp < 29936163) {
        $level = 38;
    } else if ($exp < 36147970) {
        $level = 39;
    } else if ($exp < 42137920) {
        $level = 40;
    } else if ($exp < 49355480) {
        $level = 41;
    } else if ($exp < 57959880) {
        $level = 42;
    } else if ($exp < 64372369) {
        $level = 43;
    } else if ($exp < 70382402) {
        $level = 44;
    } else if ($exp < 80382402) {
        $level = 45;
    } else if ($exp < 90382402) {
        $level = 46;
    } else if ($exp < 100382403) {
        $level = 47;
    } else if ($exp < 111382404) {
        $level = 48;
    } else if ($exp < 112082405) {
        $level = 49;
    } else if ($exp < 113582406) {
        $level = 50;
    } else if ($exp < 1295793938) {
        $level = 51;
    } else if ($exp < 1483863966) {
        $level = 52;
    } else if ($exp < 1693933994) {
        $level = 53;
    } else if ($exp < 1916004022) {
        $level = 54;
    } else if ($exp < 2160074050) {
        $level = 55;
    } else {
        $level = "55+";
    }
    unset($exp);
    return $level;
}

function getRusRace($race) {
    if (!$race) {
        return false;
    }
    if ($race == "ELYOS") {
        $race_rus = "Элиец";
    } else if ($race == "ASMODIANS") {
        $race_rus = "Асмодианин";
    } else {
        $race_rus = "SERVER ERROR";
    }
    unset($race);
    return $race_rus;
}

function getPositClass($race) {
    if (!$race) {
        return false;
    }
    if ($race == "ELYOS") {
        $x_class = "145";
    } else if ($race == "ASMODIANS") {
        $x_class = "180";
    }
    unset($race);
    return $x_class;
}

function getRusClass($player_class) {
    if (!$player_class) {
        return false;
    }
    if ($player_class == "WARRIOR") {
        $class_rus = "Воин";
    } else if ($player_class == "GLADIATOR") {
        $class_rus = "Гладиатор";
    } else if ($player_class == "TEMPLAR") {
        $class_rus = "Тамплиер";
    } else if ($player_class == "SCOUT") {
        $class_rus = "Бойскаут";
    } else if ($player_class == "ASSASSIN") {
        $class_rus = "Убийца";
    } else if ($player_class == "RANGER") {
        $class_rus = "Лучник";
    } else if ($player_class == "MAGE") {
        $class_rus = "Маг";
    } else if ($player_class == "SORCERER") {
        $class_rus = "Волшебник";
    } else if ($player_class == "SPIRIT_MASTER") {
        $class_rus = "Повелитель духов";
    } else if ($player_class == "PRIEST") {
        $class_rus = "Священник";
    } else if ($player_class == "CLERIC") {
        $class_rus = "Целитель";
    } else if ($player_class == "CHANTER") {
        $class_rus = "Чародей";
    } else {
        $class_rus = "ОШИБКА СЕРВЕРА";
    }
    unset($player_class);
    return $class_rus;
}

function make_players_array() {
    $conf = new Config ();
    $name = $_GET['name'];
    global $pl_array;
    $i = 0;
    $query = "SELECT * FROM `players` WHERE `name` = '$name'";
    $aux = DB::getInstance()->sql($query);
    while ($result = mysql_fetch_assoc($aux)) {
        $char_data = explode(' ', $result['data']);
        $char_gender = dechex($char_data[36]);
        $char_gender = str_pad($char_gender, 8, 0, STR_PAD_LEFT);
        $char_gender = $char_gender{3};
        $res_pol = $def['character_race'][$pol];
        $pl_array[$i] = Array($result['name'], $result['race'], $result['player_class'], $result['gender'], $result['exp'], $result['online']);
        $i++;
    }
    return $i;
}

$onlineplayers = make_players_array();

if ($onlineplayers == 0) {
    $im = imagecreatetruecolor(145, 20);
    $text_color = imagecolorallocate($im, 233, 14, 91);
    imagestring($im, 1, 5, 5, 'NO CHARACTER WITH THIS NAME', $text_color);
    header('Content-type: image/jpeg');
    imagejpeg($im, NULL, 100);
    imagedestroy($im);
    exit();
} else {
    if ($type > 22) {
        $im = imagecreatetruecolor(145, 20);
        $text_color = imagecolorallocate($im, 233, 14, 91);

        imagestring($im, 1, 5, 5, 'BACKGROUND MUST BE 1-22', $text_color);
        header('Content-type: image/jpeg');
        imagejpeg($im, NULL, 100);
        imagedestroy($im);
        exit();
    } else {
        if (!$sort = &$_GET['s'])
            $sort = 0;
        if (!$flag = &$_GET['f'])
            $flag = 0;
        if ($flag == 0) {
            $flag = 1;
            $sort_type = '<';
        } else {
            $flag = 0;
            $sort_type = '>';
        }
        $link = $_SERVER['PHP_SELF'] . "?f=" . $flag . "&s=";

        if (!empty($pl_array)) {
            usort($pl_array, create_function('$a, $b', 'if ( $a[' . $sort . '] == $b[' . $sort . '] ) return 0; if ( $a[' . $sort . '] ' . $sort_type . ' $b[' . $sort . '] ) return -1; return 1;'));
        }

        $list = "";
        $i = 0;
        while ($i < $onlineplayers) {
            $char = $pl_array[$i][0];
            $EXP = $pl_array[$i][4];
            $ONLI = $pl_array[$i][5];
            $race = $pl_array[$i][1];
            $player_class = $pl_array[$i][2];
            $pol = $pl_array[$i][3];
            $level = getLevel($EXP);
            $i++;
        }

        if ($ONLI == 0) {
            $online = "Не в игре";
        } else {
            $online = "В игре";
        }



//name
        $font_name = 20;
        $x_name = 86;
        $y_name = 23;
        $shrift = 'content/fonts/calibrib.ttf';

//name shadow
        $font_names = 20;
        $x_names = 88;
        $y_names = 24;

//level
        $font_level = 10;
        $x_level = 86;
        $y_level = 50;

//level_shadow
        $font_levelshadow = 10;
        $x_levelshadow = 87;
        $y_levelshadow = 51;

//race
        $race_rus = getRusRace($race);
        $font_race = 10;
        $x_race = 105;
        $y_race = 50;

//race_shadow
        $race_rus = getRusRace($race);
        $font_raceshadow = 10;
        $x_raceshadow = 106;
        $y_raceshadow = 51;

//class
        $class_rus = getRusClass($player_class);
        $font_class = 10;
        $x_class = getPositClass($race);
        $y_class = 50;

//class_shadow
        $class_rus = getRusClass($player_class);
        $x_classi = getPositClass($race) + 1;
        $y_classi = 51;

//Server Name
        $font_server = 8;
        $x_server = 86;
        $y_server = 65;

//Server shadow
        $x_servers = 87;
        $y_servers = 66;

//online
        $font_online = 8;
        $x_online = 86;
        $y_online = 36;

//online shadow
        $x_onlines = 87;
        $y_onlines = 37;

//Engine
        $path = 'content/images/userbars/bg/' . $type . '.jpg';
        if ($race == "ASMODIANS") {
            if ($pol == "MALE")
                $num = rand(1, 5);
            $path2 = 'content/images/userbars/face/ASMO/' . $race . '-' . $pol . '-' . "$num" . '.jpg';
            if ($pol == "FEMALE")
                $num = rand(1, 5);
            $path2 = 'content/images/userbars/face/ASMO/' . $race . '-' . $pol . '-' . "$num" . '.jpg';
        }
        if ($race == "ELYOS") {
            if ($pol == "MALE")
                $num = rand(1, 10);
            $path2 = 'content/images/userbars/face/ELY/' . $race . '-' . $pol . '-' . "$num" . '.jpg';
            if ($pol == "FEMALE")
                $num = rand(1, 9);
            $path2 = 'content/images/userbars/face/ELY/' . $race . '-' . $pol . '-' . "$num" . '.jpg';
        }
        $im = imagecreatefromjpeg($path);
        $im2 = imagecreatefromjpeg($path2);
        $foto_hw = getimagesize($path);
        $znak_hw = getimagesize($path2);

//colors
        $white = imagecolorallocate($im, 255, 255, 255);
        $black = imagecolorallocate($im, 0, 0, 0);

//name shadow
        imagettftext($im, $font_names, 0, $x_names, $y_names, $black, $shrift, $char);
//name
        imagettftext($im, $font_name, 0, $x_name, $y_name, $white, $shrift, $char);
//Level shadow
        imagettftext($im, $font_levelshadow, 0, $x_levelshadow, $y_levelshadow, $black, $shrift, $level);
//Level
        imagettftext($im, $font_level, 0, $x_level, $y_level, $white, $shrift, $level);
//race shadow
        imagettftext($im, $font_raceshadow, 0, $x_raceshadow, $y_raceshadow, $black, $shrift, $race_rus);
//race
        imagettftext($im, $font_race, 0, $x_race, $y_race, $white, $shrift, $race_rus);
//class shadow
        imagettftext($im, $font_class, 0, $x_classi, $y_classi, $black, $shrift, $class_rus);
//class
        imagettftext($im, $font_class, 0, $x_class, $y_class, $white, $shrift, $class_rus);
//Server shadow
        $conf = new Config();
        imagettftext($im, $font_server, 0, $x_servers, $y_servers, $black, $shrift, $conf->configData['servername']);
//Server Name
        imagettftext($im, $font_server, 0, $x_server, $y_server, $white, $shrift, $conf->configData['servername']);
//Online shadow
        imagettftext($im, $font_online, 0, $x_onlines, $y_onlines, $black, $shrift, $online);
//Online
        imagettftext($im, $font_online, 0, $x_online, $y_online, $white, $shrift, $online);

        imagecopy($im, $im2, $foto_hw[1] - $znak_hw[0], 0, 0, 0, $znak_hw[0], $znak_hw[1]);


        header('Content-type: image/jpeg');
        imagejpeg($im, NULL, 100);
        imagedestroy($im);
    }
}
?> 


